/*
 * @Author: Clark
 * @Email: haixuanwoTxh@gmail.com
 * @Date: 2023-01-29 13:32:30
 * @LastEditors: Clark
 * @LastEditTime: 2023-01-29 13:33:34
 * @Description: file content
 */
#include "apue.h"
#include <fcntl.h>

pid_t lock_test(int fd, int type, off_t offset, int whence, off_t len)
{
    struct flock lock;

    lock.l_type = type;     // F_RDLCK F_WRLCK F_UNLCK
    lock.l_start = offset;  // relative to l_whence
    lock.l_whence = whence; // SEEK_SET SEEK_CUR SEEK_END
    lock.l_len = len;       // 0 means to EOF

    if (fcntl(fd, F_GETLK, &lock) < 0)
        err_sys("fcntl error");

    if (F_UNLCK == lock.l_type)
    {
        return 0;   // false region isn't locked by another proc
    }

    return lock.l_pid;
}
